/** 
 * 洛克人跳跃
 */

var jumpSpeed = 1500;//跳跃的速度
var maxSpeed = 450;//最高跳跃速度
var startToReduceVelocityInY = false;

cc.Class({
    extends: cc.Component,

    properties: {
        player: cc.Node,
        main: cc.Node,
        _startJumpY: 0,

    },

    changeVelocityInY: function (value) {
        if (this.player && value != undefined) {
            var com = this.player.getComponent("player");
            com.stopAn(PlayerStates.PlayerAnimaEnemu.STAND)
            com.playAn(PlayerStates.PlayerAnimaEnemu.JUMP)
        }
    },

    update: function (dt) {
        if (InputControl.getControl(GameEvent.KEY.V)) {
            this._startJumpY += 0 + jumpSpeed * dt
            if (this._startJumpY > maxSpeed) {
                this._jumpTag = true;
                this._startJumpY -= maxSpeed;
            }
        } else if (PlayerStates.is(PlayerStates.PlayerStatesEnum.PlayerJump)) {
            this._jumpTag = true;
        }
        if (PlayerStates.is(PlayerStates.PlayerStatesEnum.PlayerJump) && !this._jumpTag) {
            this.AddVelocityToUp(this._startJumpY)
        }

    },

    AddVelocityToUp: function (ySpeed) {
        var thisRigidBody = this.player.getComponent("player").body
        var speed = thisRigidBody.linearVelocity;
        speed.y = ySpeed
        if (speed.y > maxSpeed) {
            speed.y = maxSpeed;
        }
        thisRigidBody.linearVelocity = speed
    },

    movementJumptStop: function () {
        startToReduceVelocityInY = true;

    },
    movementJumpStart: function (value) {
        var PlayerStatesEnum = PlayerStates.PlayerStatesEnum;

        this._jumpTag = false;
        if (!PlayerStates.is(PlayerStatesEnum.PlayerJump))
            PlayerStates.setPlayerState(PlayerStatesEnum.PlayerJump, true);


        startToReduceVelocityInY = false;
        //Call the function to move
        this.changeVelocityInY(value);
    },


    controlsMovementJump: function (value) {
        if (!this._jumpTag)
            this.movementJumpStart(value);
        else
            this.movementJumptStop();

    },


    onPlayerEvent: function (eventData) {
        let state = PlayerStates.getCurState();
        if (state === PlayerStates.PlayerStatesEnum.PlayerJump) {
            this._startJumpY = 0
            this._jumpTag = false
            var isGround = this.player.getComponent("player").isGround();
            if (isGround) {
                this.controlsMovementJump(0);
            }
        }
    },

    ctor: function () {
        this._lastTime = 0;
        this._jumpTag = false;
    },

    onLoad: function () {
        GameEvent.addCmd(GameEvent.PLAYER_EVENT, this.onPlayerEvent, this)
    },

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {},

    start() {

    },

    // update (dt) {},
});
